import Taro from '@tarojs/taro'

class HttpClient {
  baseURL = "https://api.lyuanbo.xyz"
  constructor() {
    console.log('HttpClient baseURL:', this.baseURL)  // 调试输出
  }
  async request(url: string, options: any = {}) {
    const response = await Taro.request({
      url: this.baseURL + url,
      method: options.method || 'GET',
      data: options.data,
      header: {
        'Content-Type': 'application/json',
        ...options.header
      }
    })

    if (response.statusCode !== 200) {
      throw new Error(`HTTP ${response.statusCode}`)
    }

    return response.data
  }

  get(url: string, params?: any) {
    return this.request(url, {method: 'GET', data: params})
  }

  post(url: string, data?: any) {
    return this.request(url, {method: 'POST', data})
  }

  put(url: string, data?: any) {
    return this.request(url, {method: 'PUT', data})
  }
}

export const request = new HttpClient()
export default request
